package sky.cin.framework.sfoc.core.common.utils;

import java.io.File;
import java.util.Objects;

public class EnvirUtils {
    
    private static String classPath = "";
    
    public static File getClassFolder(ClassLoader classLoader, Class<?> clazz) {
        // 转换包路径
        String packagePath = clazz.getPackage().toString().split(" ")[1].replace(".", "\\");
        // 获取类目录
        if ("".equals(classPath)) {
            classPath = Objects.requireNonNull(classLoader.getResource("")).getFile().substring(1);
        }
        // 获取 Application 根路径
        File resFolder = new File(classPath, packagePath);
        if (!resFolder.isDirectory()) {
            throw new RuntimeException(resFolder.getAbsolutePath() + " is not folder");
        }
        return resFolder;
    }
    
    
    public static String getClassPath() {
        return classPath;
    }
    
    public static String getClassPath(File classFile) {
        String classFileAbsolutePath = classFile.getAbsolutePath();
        int start = classFileAbsolutePath.indexOf("sky");
        int end = classFileAbsolutePath.indexOf(".class");
        return classFileAbsolutePath.substring(start, end).replace("\\", ".");
    }
    
}
